"""
Case Type   : 多节点写入
Case Name   : gs_guc set方式修改enable_remote_execute参数值
Create At   : 2023/1/4
Owner       : @li-xin12345
Description :
    1.查询默认值
    2.gs_guc set方式修改enable_remote_execute参数值为on
    3.gs_guc set方式修改enable_remote_execute参数值为0/1
    4.gs_guc set方式修改enable_remote_execute参数值为false/true
    5.gs_guc set方式修改enable_remote_execute参数值为on/off,false/true,0/1之外的值
    6.gs_guc set方式修改enable_remote_execute参数值为off
Expect      :
    1.默认值为off
    2.修改成功
    3.修改成功
    4.修改成功
    5.修改失败，合理报错
    6.修改成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class MultiNodeWrite(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.config = 'enable_remote_execute'

    def test_multi_node_write(self):
        step1 = "-----step1:查询默认值;expect:默认值为off-----"
        self.logger.info(step1)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'off', '执行失败' + step1)

        step2 = "-----step2:gs_guc set方式修改enable_remote_execute" \
                "参数值为on;expect:修改成功-----"
        self.logger.info(step2)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=on')
        self.assertTrue(result, '执行失败' + step2)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + step2)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + step2)

        step3 = "-----step3:gs_guc set方式修改enable_remote_execute" \
                "参数值为0/1;expect:修改成功-----"
        self.logger.info(step3)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=0')
        self.assertTrue(result, '执行失败' + step3)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + step3)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'off', '执行失败' + step3)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=1')
        self.assertTrue(result, '执行失败' + step3)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + step3)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + step3)

        step4 = "-----step4:gs_guc set方式修改enable_remote_execute" \
                "参数值为false/true;expect:修改成功-----"
        self.logger.info(step4)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=false')
        self.assertTrue(result, '执行失败' + step4)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + step4)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'off', '执行失败' + step4)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=true')
        self.assertTrue(result, '执行失败' + step4)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + step4)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + step4)

        step5 = "-----step5:gs_guc set方式修改enable_remote_execute" \
                "参数值为on/off,false/true,0/1之外的值;expect:修改失败，合理报错-----"
        self.logger.info(step5)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=ab')
        self.assertFalse(result, '执行失败' + step5)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=123')
        self.assertFalse(result, '执行失败' + step5)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=@on*')
        self.assertFalse(result, '执行失败' + step5)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=#23$')
        self.assertFalse(result, '执行失败' + step5)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + step5)

    def tearDown(self):
        step6 = "-----step6:gs_guc set方式修改enable_remote_execute" \
               "参数值为off;expect:修改成功-----"
        self.logger.info(step6)
        guc_res = self.primary_sh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'{self.config}=off')
        restart_res = self.primary_sh.restart_db_cluster()
        show_msg = self.common.show_param(self.config)
        self.assertTrue(guc_res, '执行失败' + step6)
        self.assertTrue(restart_res, '执行失败' + step6)
        self.assertEquals(show_msg, 'off', '执行失败' + step6)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
